package com.rainner.app.action;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.app.base.action.JsonAction;
import com.rainner.app.server.BookServer;

@Results({ @Result(name = "BOOKDETAIL", type = "freemarker", location = "/WEB-INF/content/public/details.ftl"), @Result(name = "OUTLOGIN", type = "redirect", location = "/login.html"), @Result(name = "ERROR", type = "redirect", location = "/error.html") })
public class DetailAction extends JsonAction {
	private static final long serialVersionUID = 1L;
	private BookServer bookServer = new BookServer();
	private String bookId;

	@Action("/detail" + "/{bookId}")
	public String detailBook() throws SQLException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object username = session.getAttribute("username");
		if (username == null) {
			return "OUTLOGIN";
		} else {
			map.put("username", username.toString());
			if (bookId == null) {
				return "ERROR";
			} else {
				map.put("bookdetail", bookServer.bookDataByBookId(bookId));
			}
			return "BOOKDETAIL";
		}
	}

	public void setBookId(String bookId) {
		this.bookId = bookId;
	}

}
